import 'elix/define/Dialog.js'
import ykHtml from './ykverify.html'
import { pwsdpage } from './pwsd.js'


import {checkYK} from "./ykAPI.js"

const $click = (element, handler) => {
    element.addEventListener('click', handler)
    element.addEventListener('touchend', handler)
  }
export class ykverify extends HTMLElement{
    constructor(svgShow){
        super()
        const template = document.createElement('template')
        template.innerHTML = ykHtml
        this.svgShow = svgShow
        this.svgShow.$svgShow.append(template.content.cloneNode(true))
        this.$docDialog = this.svgShow.$svgShow.querySelector("#yk_box")
        this.$container = this.svgShow.$svgShow.querySelector("#container3")
        this.$pwsd = new pwsdpage(svgShow,true)
        this.init()
    }

    open(text,elem){
        this.action = text
        this.elem = elem
        this.svgShow.$svgShow.querySelector("#name_yk").textContent = elem.doc.name
        this.svgShow.$svgShow.querySelector("#device_yk").textContent = elem.doc.device
        this.svgShow.$svgShow.querySelector("#state_yk").textContent = elem.doc.state
        this.svgShow.$svgShow.querySelector("#index_yk").textContent = elem.doc.device + " " + (text== "#svg-F"?"分":"合")
        this.svgShow.$svgShow.querySelector('#yuming_yk').innerHTML = text == "#svg-H"?"分":"合"
 
        var arr = elem.doc.data.split(":");
        var id = arr[1]?arr[1]:'';
        var obj ={
            "cb_id":id
        }

        checkYK(obj).then(res=>{
            console.log(res)
            if(res.data.data && res.data.data.yk_type=="2"){
                svGShow.yaokongyuzhiBol = true;
            }else{
                svGShow.yaokongyuzhiBol = false;
            }
           console.log(res)
           this.initBtn()
           this.$docDialog.open()
        }).catch(res=>{
           this.initBtn()
           this.$docDialog.open()
        })
        // axios.get(window.serviceUrl + "/doc/define/yk",{params:{
        //     "cb_id":id
        // }}).then(res=>{
           

        // })
        
    }

    init(){
        this.$close = this.svgShow.$svgShow.querySelector('.closeYk')
        this.$pwsd1 = this.svgShow.$svgShow.querySelector('#pwsd1')
        this.$pwsd2 = this.svgShow.$svgShow.querySelector('#pwsd2')
        this.$ykyz = this.svgShow.$svgShow.querySelector('#ykyz')
        this.$ykzx = this.svgShow.$svgShow.querySelector('#ykzx')
        this.ykStep =0;
        this.bt1_val = this.$pwsd1.value
        this.bt2_val = this.$pwsd2.value
        this.bt3_val = this.$ykyz.value
        this.bt4_val = this.$ykzx.value
        var that = this
        $click(this.$close, function () {
            svGShow.lastName = null
            that.$docDialog.close()
        })
        this.$closebtn = this.svgShow.$svgShow.querySelector('.cls_bot_btn')
        $click(this.$closebtn, function () {
            svGShow.lastName = null
            that.$docDialog.close()
        })
        $click(this.$pwsd1, function () {
            if(that.ykStep !=0){
                return
            }
            that.$pwsd.open(that)
        })
        $click(this.$pwsd2, function () {
            if(that.ykStep !=1){
                return
            }
            that.$pwsd.open(that)
        })
        $click(this.$ykyz, function () {
            if(that.ykStep !=2){
                return
            }
            that.ykStep = 3
            that.elem.ykcmd("ykyz",{cmdType:that.action},that)
            svGShow.loadingpage.open()
        })
        $click(this.$ykzx, function () {
            if(that.ykStep !=3){
                return
            }
            that.elem.ykcmd("ykzx",{cmdType:that.action},that)
            svGShow.loadingpage.open()
        })
    }

    setBtnYzSuccess(){
        this.$ykyz.className = "cls_button_clkd"
        this.$ykyz.value += "成功" 
        this.$ykyz.disabled = true
        this.$ykzx.disabled = false
    }

    setBtnZxSuccess(){
        this.$ykzx.className = "cls_button_clkd_suc"
        this.$ykzx.value += "成功" 
        this.$ykyz.disabled = true
        this.$ykzx.disabled = true
    }

    initBtn(){
        this.ykStep =0;
        this.$pwsd1.disabled = false
        this.$pwsd2.disabled = true
        this.$ykyz.disabled = true
        this.$ykzx.disabled = true

        this.$pwsd1.value = this.bt1_val
        this.$pwsd2.value = this.bt2_val
        this.$ykyz.value = this.bt3_val
        this.$ykzx.value = this.bt4_val

        this.$pwsd1.className = "cls_button_clkd"
        this.$pwsd2.className = "cls_button"
        this.$ykyz.className = "cls_button"
        this.$ykzx.className = "cls_button"

        if(svGShow.isDebugger){
            this.$pwsd1.className = "cls_button"
            this.$pwsd2.className = "cls_button"
            this.$pwsd1.disabled = true
            this.$pwsd2.disabled = true
            this.$ykyz.disabled = false
            this.$ykzx.disabled = true
            // this.$ykyz.disabled = true
            // this.$ykzx.disabled = false
            this.ykStep =2;
        }
    }

}
customElements.define('yk-dialog', ykverify)

